package com.service;
import com.dao.airticle.ArticleDao;
import com.dao.airticle.ColumnDao;
import com.dao.airticle.LabelDao;
import com.dao.airticle.PublishBlog;
import com.pojo.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class ArticleService {
       /**
        *发布博客
        */
       public static String publish_blog(HttpServletRequest req) throws IOException {
              Article article=new Article();
              Date date = new Date();
              SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
              article.setArticle_author(req.getParameter("author_id"));
              article.setArticle_content(req.getParameter("md_id"));
              article.setArticle_title(req.getParameter("title"));
              article.setCreate_time(dateFormat.format(date));
              article.setIs_original(req.getParameter("is_original"));
              article.setIs_delete("0");
              article.setIs_draft("0");
              article.setIs_check("0");
              article.setContent_text(req.getParameter("content_text"));
              /*获取到文章id*/
              String articleId=PublishBlog.publish_blog(article);

              String[] column_list=req.getParameterValues("column[]");
              if(column_list!=null)
              PublishBlog.publish_blog_column(column_list,articleId);

              String[] label_list=req.getParameterValues("label[]");
              if(label_list!=null)
              PublishBlog.publish_blog_label(label_list,articleId);

              return articleId;
       }

    /**
     * 保存草稿
     */
     public static void save_draft(HttpServletRequest req)throws IOException{
        Article article=new Article();
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        article.setArticle_author(req.getParameter("author_id"));
        article.setArticle_content(req.getParameter("md_id"));
        article.setArticle_title(req.getParameter("title"));
        article.setCreate_time(dateFormat.format(date));
        article.setIs_original(req.getParameter("is_original"));
        article.setIs_delete("0");
        article.setIs_draft("1");
        article.setIs_check("0");
        article.setContent_text(req.getParameter("content_text"));

        String articleId=PublishBlog.save_draft(article);

        String[] column_list=req.getParameterValues("column[]");
        if(column_list!=null)
            PublishBlog.publish_blog_column(column_list,articleId);

        String[] label_list=req.getParameterValues("label[]");
        if(label_list!=null)
            PublishBlog.publish_blog_label(label_list,articleId);
    }

    /**
     * 编辑博客
     */
     public static void editor_blog(HttpServletRequest req)throws IOException{
             Article article=new Article();
             /*记录更新时间*/
             Date date = new Date();
             SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
             article.setArticle_content(req.getParameter("md_id"));
             article.setArticle_title(req.getParameter("title"));
             article.setUpdate_time(dateFormat.format(date));
             article.setIs_original(req.getParameter("is_original"));
             article.setContent_text(req.getParameter("content_text"));
             article.setId(req.getParameter("blog_id"));
             article.setIs_draft(req.getParameter("is_draft"));
             article.setIs_check(req.getParameter("is_check"));

             ArticleDao.editor_blog(article);

             ColumnDao.del_blog_column(article.getId());
             String[] column_list=req.getParameterValues("column[]");
             if(column_list!=null)
             PublishBlog.publish_blog_column(column_list,article.getId());

             LabelDao.del_blog_label(article.getId());
             String[] label_list=req.getParameterValues("label[]");
             if(label_list!=null)
             PublishBlog.publish_blog_label(label_list,article.getId());
     }

    /**
     * 新建专栏
        */
       public static void add_column(HttpServletRequest req)throws  IOException{
               String column_name=req.getParameter("column_name");
               String userid= req.getParameter("email");
               ColumnDao.add_column(column_name,userid);
       }
       /**
        * 获取专栏
        */
       public static List<String> get_column_list(HttpServletRequest req)throws IOException{
                return ColumnDao.get_column_list(req.getParameter("email"));
       }

       /**
        * 自定义标签
        */
       public static void add_label(HttpServletRequest req)throws IOException{
                 String label_name=req.getParameter("label_name");
                 LabelDao.add_label(label_name);
       }

       /**
        * 查找标签
        */
       public static List<String> search_label(HttpServletRequest req)throws IOException{
               String label_name=req.getParameter("search_label");
               return  LabelDao.search_label("%"+label_name+"%");
       }

       /**
        *获取文章信息
        */
       public static Article get_blog_content(HttpServletRequest req)throws IOException{
                String blog_id=req.getParameter("id");
                return PublishBlog.get_blog_content(blog_id);
       }

       /**
        * 获取当前文章专栏
        */
       public static List<String> get_blog_column(HttpServletRequest req)throws IOException{
                return ColumnDao.get_blog_column(req.getParameter("blog_id"));
       }

      /**
       * 获取当前文章表亲啊
       */
      public static List<String> get_blog_label(HttpServletRequest req)throws IOException{
          return LabelDao.get_blog_label(req.getParameter("blog_id"));
      }

    /**
     * 修改专栏名
     */
     public static void change_column_name(HttpServletRequest req)throws IOException{
            String new_name=req.getParameter("new_column_name");
            String old_name=req.getParameter("old_column_name");
            ColumnDao.change_column_name(new_name,old_name);
            ColumnDao.change_article_column(new_name,old_name);
     }

    /**
     * 删除专栏
     */
    public static void del_column(HttpServletRequest req)throws IOException{
              String column_name=req.getParameter("column_name");
              ColumnDao.del_column(column_name);
              ColumnDao.del_article_column(column_name);
    }

    /**
     * 获取专栏中的博客
     */
    public static List<Article> get_column_article(HttpServletRequest req)throws IOException{
           String column_name=req.getParameter("column_name");
           return ColumnDao.get_column_article(column_name);
    }

    /**
     * 移除专栏中的文章
     */
    public static void del_column_blog(HttpServletRequest req)throws IOException{
               String blog_id=req.getParameter("blog_id");
               String column_name=req.getParameter("column_name");
               ColumnDao.del_column_blog(blog_id,column_name);
    }

    /**
     * 获取所有博客
     */
     public static List<Article> get_all_blog(HttpServletRequest req)throws IOException{
         MangeArticle mangeArticle=new MangeArticle();
          String year=req.getParameter("year");
          String month=req.getParameter("month");
          String article_type=req.getParameter("article_type");
          String column=req.getParameter("article_column");
          String article_title=req.getParameter("article_title");
          String year_condition="%";
          String month_condition="%";
          String time_condition;
          if(!Objects.equals(year, "年")&& !year.equals("不限")){
              year_condition=year;
          }
          if(!Objects.equals(month, "月")&& !month.equals("不限"))
          {
              month_condition=month.substring(0,month.indexOf("月"));
              if(!month_condition.equals("10") && !month_condition.equals("11") && !month_condition.equals("12")){
                  month_condition="0"+month_condition;
              }
          }
          time_condition=year_condition+"-"+month_condition+"-%";
          mangeArticle.setCreate_time(time_condition);
          mangeArticle.setArticle_author(req.getParameter("email"));
          mangeArticle.setIs_delete("0");
          mangeArticle.setIs_draft("0");
          mangeArticle.setIs_check("1");
          if(!Objects.equals(article_title, ""))
          {
              mangeArticle.setArticle_title("%"+article_title+"%");
          }
          if(!Objects.equals(article_type, "文章类型") && !Objects.equals(article_type, "不限"))
          {
              mangeArticle.setIs_original(article_type);
          }
          if(!Objects.equals(column, "不限") && !Objects.equals(column, "分类专栏"))
          {
              mangeArticle.setColumn_name(column);
          }
          int page_number=Integer.parseInt(req.getParameter("page_number"));
          mangeArticle.setPage_number(page_number*8);
          return ArticleDao.get_all_blog(mangeArticle);
     }

    /**
     * 删除博客
     */
     public static void del_blog(HttpServletRequest req)throws IOException{
             String blog_id=req.getParameter("blog_id");
             ArticleDao.del_blog(blog_id);
     }

    /**
     * 获取近期文章
     */
     public static List<Article> get_recent_article(HttpServletRequest req)throws IOException{
               String article_author=req.getParameter("email");
               return ArticleDao.get_recent_blog(article_author);
     }

    /**
     * 获取专栏作者信息
     */
    public static Author get_column_author(HttpServletRequest req)throws IOException{
             String column_name=req.getParameter("column_name");
             String self_id=req.getParameter("self_id");
             return ColumnDao.get_column_author(column_name,self_id);
    }

    /**
     * 获取专栏详情中的博客
     */
    public static List<Article> get_column_info_blog(HttpServletRequest req) throws  IOException{
            String column_name=req.getParameter("column_name");
            int page_number=Integer.parseInt(req.getParameter("page"));
            return ArticleDao.get_column_info_blog(page_number*6,column_name);
    }

    /**
     * 专栏查重
     */
    public static boolean check_column(HttpServletRequest req)throws IOException{
            String column_name=req.getParameter("column_name");
            return ColumnDao.check_column(column_name);
    }

    /**
     * 获取作者专栏
     */
    public static List<Column> get_author_column(HttpServletRequest req)throws IOException{
             String column_author=req.getParameter("author_id");
             return ColumnDao.get_author_column(column_author);
    }

    /**
     * 点赞博客
     */
    public static void support_blog(HttpServletRequest req)throws IOException{
               String blog_id=req.getParameter("blog_id");
               String userid=req.getParameter("user_id");
               /*判断数据是否存在*/
               if(ArticleDao.check_support(blog_id,userid)){
                      /*数据不存在就创建数据*/
                      ArticleDao.add_support(blog_id,userid);
               }else{
                   /*如果存在就改变点赞状态*/
                    ArticleDao.change_support_status(blog_id,userid,"0");
               }
    }

    /**
     * 取消点赞博客
     */
    public static void cancel_support_blog(HttpServletRequest req)throws IOException{
           String blog_id=req.getParameter("blog_id");
           String userid=req.getParameter("user_id");
           ArticleDao.change_support_status(blog_id,userid,"1");
    }

    /**
     * 收藏博客
     */
    public static void collection_blog(HttpServletRequest req)throws IOException{
           String blog_id=req.getParameter("blog_id");
           String userid=req.getParameter("user_id");
          if(ArticleDao.check_collection(blog_id,userid)){
              /*如果收藏数据不存在，就添加*/
             ArticleDao.add_collection(blog_id,userid);
          }else{
                /*如果存在,就更改收藏状态*/
               ArticleDao.change_collection_status(blog_id,userid,"0");
          }
    }
    /**
     * 取消收藏博客
     */
    public static void cancel_collection_blog(HttpServletRequest req)throws IOException{
        String blog_id=req.getParameter("blog_id");
        String userid=req.getParameter("user_id");
        ArticleDao.change_collection_status(blog_id,userid,"1");
    }

    /**
     * 获取用户是否收藏文章
     */
    public static Operation get_user_operation(HttpServletRequest req)throws IOException{
           String blog_id=req.getParameter("blog_id");
           String userid=req.getParameter("user_id");
        return ArticleDao.get_user_is_collection(blog_id,userid);
    }

    /**
     * 获取用户是否点赞文章
     */
    public static Operation get_user_is_support(HttpServletRequest req)throws IOException{
        String blog_id=req.getParameter("blog_id");
        String userid=req.getParameter("user_id");
        return ArticleDao.get_user_operation(blog_id,userid);
    }

    /**
     * 获取收藏列表
     */
    public static List<Article> get_collection_list(HttpServletRequest req)throws IOException{
             String userid=req.getParameter("email");
             return ArticleDao.get_collection_list(userid);
    }

    /**
     * 获取草稿列表
     */
    public static List<Article> get_draft_list(HttpServletRequest req)throws IOException{
         String article_author=req.getParameter("email");
         return ArticleDao.get_draft_list(article_author);
    }

    /**
     * 获取热门推荐
     */
    public static List<Article> get_hot_blog()throws IOException{
                return ArticleDao.get_hot_blog();
    }

    /**
     * 获取最新文章
     */
    public static List<Article> get_latest_blog()throws IOException{
           return ArticleDao.get_latest_blog();
    }

    /**
     * 获取未审核文章
     */
     public static PageBean<Article> get_not_check_blog(int currentPage)throws IOException{
            PageBean<Article>pb=new PageBean<Article>();
            pb.setCurrentPage(currentPage);
            pb.setPageSize(8);

            int totalCount=ArticleDao.get_not_check_blog_count();
            pb.setTotalCount(totalCount);

            int start=(currentPage-1)*8;
            List<Article>list=ArticleDao.get_current_not_check(start);
            pb.setList(list);

           int totalPage=totalCount%8==0?totalCount/8:(totalCount/8+1);
           pb.setTotalPage(totalPage);

           return pb;
     }

    /**
     * 获取管理的文章
     */
     public static PageBean<Article> get_manage_blog(int currentPage)throws IOException{
         PageBean<Article>pb=new PageBean<Article>();
         pb.setCurrentPage(currentPage);
         pb.setPageSize(10);


         int totalCount=ArticleDao.get_manage_blog_count();
         pb.setTotalCount(totalCount);

         int start=(currentPage-1)*10;
         List<Article>list=ArticleDao.get_all_article(start);
         pb.setList(list);

         int totalPage=totalCount%10==0?totalCount/10:(totalCount/10+1);
         pb.setTotalPage(totalPage);

         return pb;
     }
}



